package process_demo.thread_safe.exclusive;

/**
 * 锁
 * @author haomin
 * @date 2022/04/22 19:21
 **/
public class Main {
    // 这个对象用来当锁对象
    static Object lock = new Object();

    static class MyThread1 extends Thread {
        @Override
        public void run() {
            synchronized (lock) {
                for (int i = 0; i < 1_0000; i++) {
                    System.out.println("对不起，我错了，下次我还敢");
                }
            }
        }
    }

    static class MyThread2 extends Thread {
        @Override
        public void run() {
            synchronized (lock) {
                for (int i = 0; i < 1_0000; i++) {
                    System.out.println("我是李四");
                }
            }
        }
    }
    static class MyThread3 extends Thread{
        @Override
        public void run() {
            // 没加synchronized 没有锁，不受保护
            for (int i = 0; i < 1_0000; i++) {
                System.out.println("我是王五");
            }
        }
    }

    public static void main(String[] args) {
        Thread t1 = new MyThread1();
        t1.start();


        Thread t2 = new MyThread2();
        t2.start();

        Thread t3 = new MyThread3();
        t3.start();
    }
}